* Table C3: Summary statistics on applicants' characteristics

********************************************************************************
************************** Prepare data ****************************************
********************************************************************************

clear all
use disadv grossincome ses budapest county_town town village GPA11_3 gender rollength program_number using "${data_students}", clear

********************************************************************************
************************** Prepare table ***************************************
********************************************************************************

cap file close sumstat
file open sumstat using "${tableC3}", write replace
file write sumstat "\begin{tabular}{l c c c} \\ \hline\hline" _n
file write sumstat " & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{SD} & \multicolumn{1}{c}{N} \\ " _n
file write sumstat " & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} \\ \hline" _n

foreach var of varlist disadv grossincome ses budapest county_town town village ///
	GPA11_3 gender rollength program_number {
	local lab_var: variable label `var'
	quietly: sum `var'
	local stat_mean: 	display %4.2f r(mean)
	local stat_sd: 		display %4.2f r(sd)
	local stat_N: 		display %12.0fc r(N)
	
	if "`vars'" == "program_number" {
		file write sumstat " `lab_var' & `stat_mean' & `stat_sd' & `stat_N' \\ \hline\hline" _n
	}
	else {
		file write sumstat " `lab_var' & `stat_mean' & `stat_sd' & `stat_N' \\ " _n
	}
}

qui: count if town == .
local stat_town_N: display %12.0fc r(N)
qui: count if ses  == .
local stat_ses_N: display %12.0fc r(N)
count if GPA11_3 == .
qui: local stat_gpa_N: display %12.0fc r(N)

file write sumstat "\hline\hline \multicolumn{4}{p{13cm}}{{\it Notes}: The table reports mean values and standard deviations of applicant characteristics. Disadvantaged status is an indicator for claiming priority points for disadvantaged status. 11th-grade GPA is the average grades in mathematics, history, and Hungarian grammar and literature. Applicants' settlement of residence is missing for $`stat_town_N'$ applicants, NABC-based SES index is missing for $`stat_ses_N'$ applicants, and 11th-grade GPA is missing for `stat_gpa_N' applicants.}"
file write sumstat "\end{tabular}" _n
file close sumstat